#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>

const char *ssid     = "Xiaomi_2G_free"; //wifi名称
const char *password = ""; //wifi密码
int button = 12;
int bee = 14;
bool button_down = 1;
bool auto_clock = 0;
WiFiUDP ntpUDP;

// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "cn.ntp.org.cn", 8 * 60 * 60, 60000);

//oled
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
// On an arduino ESP8266:   D2(SDA)GPIO4,  D1(SCL)GPIO5, ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//oled


void showtime(String time_temp) {
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0, 0);            // Start at top-left corner
  display.print(F("clock"));
  if (button_down == 1) {
    display.println(" on");
  }
  else {
    display.println(" off");
  }
  display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.println("now:");

  display.setTextSize(2);             // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  display.print(time_temp);

  display.display();

}



void setup() {
  pinMode(bee, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  Serial.begin(115200);

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
  Serial.println ( "网络时钟初始化完成" );

}

void loop() {
  timeClient.update();
  String nowtime = timeClient.getFormattedTime();
  Serial.println(nowtime);
  String hours =  nowtime.substring(0, 2);
  String minutes = nowtime.substring(3, 5);
  #String seconds = nowtime.substring(6, 8);
  Serial.println(hours);
  Serial.println(minutes);
  #Serial.println(seconds);
  if (hours == "18" and minutes == "40") {
    auto_clock = true;
  }
  else {
    auto_clock = false;
  }

  if (digitalRead(button) == 1)
  {
    Serial.println("按键已经按下了");
    button_down = !button_down;
    if (button_down == 1) {
      Serial.println("闹铃开启");
    }
    else {
      Serial.println("闹铃关闭");
    }
  }

  if (button_down == true and auto_clock == true)
  {
    Serial.println("自动闹铃");
    tone(bee, 250);
  }
  else   {
    noTone(bee);
  }


  showtime(nowtime);

  delay(1000);
}